﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DiBang.Common;
using DiBang.Sql;

namespace ChangWuDiBang
{
    public partial class FrmUserManagerModify : Office2007Form
    {
        public UserInfo _UserInfo
        {
            get;
            set;
        }

        public UserInfo _ModifyUserInfo
        {
            get;
            set;
        }

        /// <summary>
        /// 修改用户还是新增用户(ADD_NEW,MODIFY)
        /// </summary>
        public string Mode
        {
            get;
            set;
        }

        SqlLib sqllib = new SqlLib();
        SqlExtend sqlextend = new SqlExtend();

        public FrmUserManagerModify()
        {
            this.EnableGlass = false;
            InitializeComponent();
        }

        private void FrmUserManagerModify_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(Mode))
                return;

            if (_UserInfo == null)
                return;


            txtCurUserID.Text = _UserInfo.UserID;
            cmbUserGroup.Items.AddRange(new string[] { ConstParam.UserGroup_Administrator, ConstParam.UserGroup_User, 
                ConstParam.UserGroup_Viewer });

            cmbActive.Items.AddRange(new string[] { "Y","N"});
            //
            if (Mode.Equals("ADD_NEW"))
            {
                this.Text = "新增用户";
                this.txtUserID.ReadOnly = false;


            }
            else if (Mode.Equals("MODIFY"))
            {
                this.Text = "修改用户";
                this.txtUserID.ReadOnly = true;

                txtUserID.Text = _ModifyUserInfo.UserID;
                txtUserName.Text = _ModifyUserInfo.UserName;
                txtPwd.Text = Base64Encoding.UnBaseEnconding(_ModifyUserInfo.UserPwd);
                cmbUserGroup.SelectedItem = _ModifyUserInfo.UserGroup;
                cmbActive.SelectedItem = _ModifyUserInfo.Active;
            }
            else if (Mode.Equals("DELETE"))
            {
                this.Text = "删除用户";
                this.txtUserID.ReadOnly = true;
                this.btnOK.Text = "删除";

                txtUserID.Text = _ModifyUserInfo.UserID;
                txtUserName.Text = _ModifyUserInfo.UserName;
                txtPwd.Text = Base64Encoding.UnBaseEnconding(_ModifyUserInfo.UserPwd);
                cmbUserGroup.SelectedItem = _ModifyUserInfo.UserGroup;
                cmbActive.SelectedItem = _ModifyUserInfo.Active;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            int retInt = -1;
            if (!sqlextend.UserPwdCheck(txtCurUserID.Text, txtCurPwd.Text))
            {
                MessageBoxEx.Show("用户名和密码不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (Mode.Equals("ADD_NEW"))
            {
                if (!NewAddParamCheck())
                    return;

                UserInfo existUser = sqlextend.GetUserInfo(txtUserID.Text.Trim());
                if (existUser != null)
                {
                    MessageBoxEx.Show("用户已经存在,请确认!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                Dictionary<string, object> setMap = new Dictionary<string, object>();
                setMap.Add("USER_ID", txtUserID.Text.Trim());
                setMap.Add("USER_NAME", txtUserName.Text.Trim());
                setMap.Add("USER_PWD", Base64Encoding.BaseEncoding(txtPwd.Text.Trim()));
                setMap.Add("USER_GROUP", cmbUserGroup.SelectedItem.ToString());
                setMap.Add("ACTIVE", cmbActive.SelectedItem.ToString());
                setMap.Add("LAST_EVENT_NAME", Mode);
                setMap.Add("LAST_EVENT_USER_ID", txtCurUserID.Text);
                setMap.Add("LAST_EVENT_TIMEKEY", sqlextend.GetTimeKey());
                retInt=sqllib.InsertTable("DB_USER", setMap);
            }
            else if(Mode.Equals("MODIFY"))
            {
                if (!ModifyParamCheck())
                    return;

                UserInfo existUser = sqlextend.GetUserInfo(txtUserID.Text.Trim());
                if (existUser == null)
                {
                    MessageBoxEx.Show("用户已经被删除,请确认!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                Dictionary<string, object> setMap = new Dictionary<string, object>();
                //setMap.Add("USER_ID", txtUserID.Text.Trim());
                setMap.Add("USER_NAME", txtUserName.Text.Trim());
                setMap.Add("USER_PWD", Base64Encoding.BaseEncoding(txtPwd.Text.Trim()));
                setMap.Add("USER_GROUP", cmbUserGroup.SelectedItem.ToString());
                setMap.Add("ACTIVE", cmbActive.SelectedItem.ToString());
                setMap.Add("LAST_EVENT_NAME", Mode);
                setMap.Add("LAST_EVENT_USER_ID", txtCurUserID.Text);
                setMap.Add("LAST_EVENT_TIMEKEY", sqlextend.GetTimeKey());

                Dictionary<string, object> whereMap = new Dictionary<string, object>();
                whereMap.Add("USER_ID", txtUserID.Text.Trim());
                retInt=sqllib.UpdateTable("DB_USER", setMap, whereMap);
            }
            else if (Mode.Equals("DELETE"))
            {
                Dictionary<string, object> whereMap = new Dictionary<string, object>();
                whereMap.Add("USER_ID", txtUserID.Text.Trim());
                retInt = sqllib.DeleteTable("DB_USER",whereMap);
            }

            if (retInt >= 0)
            {
                MessageBoxEx.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private bool NewAddParamCheck()
        {
            if (string.IsNullOrEmpty(txtUserID.Text.Trim()))
            {
                MessageBoxEx.Show("用户ID不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUserID.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                MessageBoxEx.Show("用户名不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUserName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtPwd.Text.Trim()))
            {
                MessageBoxEx.Show("密码不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPwd.Focus();
                return false;
            }
            if (cmbUserGroup.SelectedIndex == -1)
            {
                MessageBoxEx.Show("请选择权限群组!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cmbUserGroup.Focus();
                return false;
            }
            if (cmbActive.SelectedIndex == -1)
            {
                MessageBoxEx.Show("请选择是否可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cmbActive.Focus();
                return false;
            }
            return true;
        }

        private bool ModifyParamCheck()
        {
            //if (string.IsNullOrEmpty(txtUserID.Text.Trim()))
            //{
            //    MessageBoxEx.Show("用户ID不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //    txtUserID.Focus();
            //    return false;
            //}
            if (string.IsNullOrEmpty(txtUserName.Text.Trim()))
            {
                MessageBoxEx.Show("用户名不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtUserName.Focus();
                return false;
            }
            if (string.IsNullOrEmpty(txtPwd.Text.Trim()))
            {
                MessageBoxEx.Show("密码不能为空!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPwd.Focus();
                return false;
            }
            if (cmbUserGroup.SelectedIndex == -1)
            {
                MessageBoxEx.Show("请选择权限群组!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cmbUserGroup.Focus();
                return false;
            }
            if (cmbActive.SelectedIndex == -1)
            {
                MessageBoxEx.Show("请选择是否可用!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cmbActive.Focus();
                return false;
            }
            return true;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtCurPwd_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode!=Keys.Enter)
                return;

            int retInt = -1;
            if (!sqlextend.UserPwdCheck(txtCurUserID.Text, txtCurPwd.Text))
            {
                MessageBoxEx.Show("用户名和密码不正确!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            if (Mode.Equals("ADD_NEW"))
            {
                if (!NewAddParamCheck())
                    return;

                UserInfo existUser = sqlextend.GetUserInfo(txtUserID.Text.Trim());
                if (existUser != null)
                {
                    MessageBoxEx.Show("用户已经存在,请确认!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                Dictionary<string, object> setMap = new Dictionary<string, object>();
                setMap.Add("USER_ID", txtUserID.Text.Trim());
                setMap.Add("USER_NAME", txtUserName.Text.Trim());
                setMap.Add("USER_PWD", Base64Encoding.BaseEncoding(txtPwd.Text.Trim()));
                setMap.Add("USER_GROUP", cmbUserGroup.SelectedItem.ToString());
                setMap.Add("ACTIVE", cmbActive.SelectedItem.ToString());
                setMap.Add("LAST_EVENT_NAME", Mode);
                setMap.Add("LAST_EVENT_USER_ID", txtCurUserID.Text);
                setMap.Add("LAST_EVENT_TIMEKEY", sqlextend.GetTimeKey());
                retInt = sqllib.InsertTable("DB_USER", setMap);
            }
            else if (Mode.Equals("MODIFY"))
            {
                if (!ModifyParamCheck())
                    return;

                UserInfo existUser = sqlextend.GetUserInfo(txtUserID.Text.Trim());
                if (existUser == null)
                {
                    MessageBoxEx.Show("用户已经被删除,请确认!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                Dictionary<string, object> setMap = new Dictionary<string, object>();
                //setMap.Add("USER_ID", txtUserID.Text.Trim());
                setMap.Add("USER_NAME", txtUserName.Text.Trim());
                setMap.Add("USER_PWD", Base64Encoding.BaseEncoding(txtPwd.Text.Trim()));
                setMap.Add("USER_GROUP", cmbUserGroup.SelectedItem.ToString());
                setMap.Add("ACTIVE", cmbActive.SelectedItem.ToString());
                setMap.Add("LAST_EVENT_NAME", Mode);
                setMap.Add("LAST_EVENT_USER_ID", txtCurUserID.Text);
                setMap.Add("LAST_EVENT_TIMEKEY", sqlextend.GetTimeKey());

                Dictionary<string, object> whereMap = new Dictionary<string, object>();
                whereMap.Add("USER_ID", txtUserID.Text.Trim());
                retInt = sqllib.UpdateTable("DB_USER", setMap, whereMap);
            }

            if (retInt >= 0)
            {
                MessageBoxEx.Show("操作成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
    }
}
